package com.originskyseed.platform.login.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.filter.util.FilterCacheUtil;
import com.originskyseed.platform.core.filter.util.FilterSystemTypeUtil;
import com.originskyseed.platform.core.filter.vo.SingleSignOnToken;
import com.originskyseed.platform.login.i18n.LoginResponseMsg;
import com.originskyseed.platform.login.vo.response.InvalidateResponseVO;

/**
 * 登出Controller类
 * 
 * @author LiangMingHui
 *
 */
@Controller
@RequestMapping("/logout")
@BaseLocaleResource(localeFileName = "i18n_login")
public class LogoutController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(LogoutController.class);

	/**
	 * 登出
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SysLog(module = "登出", operationDesc = "退出登录")
	@ResponseBody
	@RequestMapping("/invalidate.json")
	public BaseResponseBody invalidate(HttpServletRequest request, HttpServletResponse response) {
		BaseResponseBody result = null;
		try {
			// 获取登录凭证
			SingleSignOnToken ssoToken = SingleSignOnTokenHelper.getSingleSignOnToken();
			if (null != ssoToken) {

				// 获取客户端系统类型
				String clientSystemType = FilterSystemTypeUtil.getInstance().getClientSystemType(request, response);

				// 清空相关缓存
				FilterCacheUtil.removeCache(clientSystemType, ssoToken.getId());

			}

			// 返回登出响应VO
			InvalidateResponseVO responseVO = new InvalidateResponseVO();
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.LOGOUT_SUCCESS.getMsg()), responseVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.LOGOUT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
